﻿/*--
标题：CSDN API调用
设计：王集鹄
博客：http://blog.csdn.net/zswang
日期：2010年4月20日
--*/

function $(id) { return document.getElementById(id); }
function format(template, json) {
	if (!json) return template;
	return template.replace(/\$\{(.+?)\}/g, function() {
		return json[arguments[1]];
	});
} 

function xmlEncode(str) {
	return str.replace(/[\x00-\x60]/g, function() {
		return ["&#", arguments[0].charCodeAt(0), ";"].join("");
	});
}

var CSDN = {
	apiUrl: "http://forum.csdn.net/OpenApi/forumapi.asmx",
	post: function(options) {
		var result = {};
		options = options || {};
		options.isAskExpert = options.expert ? "true" : "false";
		options.subject = xmlEncode(options.subject);
		options.body = xmlEncode(options.body);
		options.password = xmlEncode(options.password);
		var contentText = format([
'<?xml version="1.0" encoding="utf-8"?>',
'<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">',
'	<soap12:Body>',
'		<Post xmlns="http://www.csdn.net/">',
'			<identity>',
'				<username>${username}</username>',
'				<password>${password}</password>',
'			</identity>',
'			<post>',
'				<forumId>${forumId}</forumId>',
'				<subject>${subject}</subject>',
'				<body>${body}</body>',
'				<tag>${tag}</tag>',
'				<point>${point}</point>',
'				<isAskExpert>${isAskExpert}</isAskExpert>',
'				<expertUserName>${expert}</expertUserName>',
'				<editor>UBB</editor>',
'				<url></url>',
'			</post>',
'		</Post>',
'	</soap12:Body>',
'</soap12:Envelope>'].join("\n"), options);
		var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		try {
			xmlhttp.open("POST", this.apiUrl, false);
			xmlhttp.setRequestHeader("CONTENT-LENGTH", contentText.length);
			xmlhttp.setRequestHeader("CONTENT-TYPE", "text/xml; charset=utf-8");
			xmlhttp.send(contentText);
			var xml = xmlhttp.responseText;
			var match = xml.match(/<errId>(.*?)<\/errId>/);
			if (match) result.errId = +match[1];
			match = xml.match(/<errInfo>(.*?)<\/errInfo>/);
			if (match) result.errInfo = match[1];
			match = xml.match(/<topicUrl>(.*?)<\/topicUrl>/);
			if (match) result.topicUrl = match[1];
		} catch (ex) {
			return { errId: 1, errInfo: ex.message };
		} finally {
			xmlhttp = null;
		}
		return result;
	},
	reply: function(options) {
		var result = {};
		options = options || {};
		options.body = xmlEncode(options.body);
		options.password = xmlEncode(options.password);
		var contentText = format([
'<?xml version="1.0" encoding="utf-8"?>',
'<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">',
'	<soap12:Body>',
'		<Reply xmlns="http://www.csdn.net/">',
'			<identity>',
'				<username>${username}</username>',
'				<password>${password}</password>',
'			</identity>',
'			<reply>',
'				<forumId>${forumId}</forumId>',
'				<topicUrl>${topicUrl}</topicUrl>',
'				<body>${body}</body>',
'				<editor>UBB</editor>',
'			</reply>',
'		</Reply>',
'	</soap12:Body>',
'</soap12:Envelope>'].join("\n"), options);
	},
	getUserProfile: function(options) {
		var result = {};
		options = options || {};
		options.password = xmlEncode(options.password);
		var contentText = format([
'<?xml version="1.0" encoding="utf-8"?>',
'<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">',
'	<soap12:Body>',
'		<GetUserProfile xmlns="http://www.csdn.net/">',
'			<identity>',
'				<username>${username}</username>',
'				<password>${password}</password>',
'			</identity>',
'			<username>${destuser}</username>',
'		</GetUserProfile>',
'	</soap12:Body>',
'</soap12:Envelope>\n'].join("\n"), options);
		var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		try {
			xmlhttp.open("POST", this.apiUrl, false);
			xmlhttp.setRequestHeader("CONTENT-LENGTH", contentText.length);
			xmlhttp.setRequestHeader("CONTENT-TYPE", "text/xml; charset=utf-8");
			xmlhttp.send(contentText);
			var xml = xmlhttp.responseText;
			var match = xml.match(/<errId>(.*?)<\/errId>/);
			if (match) result.errId = +match[1];
			match = xml.match(/<errInfo>(.*?)<\/errInfo>/);
			if (match) result.errInfo = match[1];
			match = xml.match(/<point>(.*?)<\/point>/);
			if (match) result.point = +match[1];
			match = xml.match(/<nickName>(.*?)<\/nickName>/);
			if (match) result.nickName = match[1];
			match = xml.match(/<username>(.*?)<\/username>/);
			if (match) result.username = match[1];
			match = xml.match(/<techExpertPoint>(.*?)<\/techExpertPoint>/);
			if (match) result.techExpertPoint = +match[1];
			match = xml.match(/<nonTechExpertPoint>(.*?)<\/nonTechExpertPoint>/);
			if (match) result.nonTechExpertPoint = +match[1];
		} catch (ex) {
			return { errId: 1, errInfo: ex.message };
		} finally {
			xmlhttp = null;
		}
		return result;
	}
};